package javafx191labs.ssheypak.seminar01.gui;

import javafx.scene.CustomNode;

import javafx.scene.Node;
import javafx.scene.Group;
import javafx.ext.swing.SwingLabel;
import javafx.ext.swing.SwingComponent;
import javafx.ext.swing.SwingVerticalAlignment;
import javafx.ext.swing.SwingButton;
import javafx.scene.input.MouseEvent;
import javafx191labs.ssheypak.common.data.Entity;
import javafx191labs.ssheypak.common.EntityReflector;
import javafx.ext.swing.SwingTextField;
import javafx191labs.ssheypak.common.data.DataStoreManager;
import java.util.Map;
import java.util.HashMap;
import java.util.HashMap;

/**
 * Form for storing or updating persons.
 * @author ssheipak
 */

public class CreatePersonForm extends CustomNode{

    /** Instance of underlying model. */
    public var model: Entity;

    /** Form title. */
    public var title: String;

    /** Form descr. hint. */
    public var description: String;

    /**Form rows with label titles and ui controls. */
    public var formRows : FormRow[];

    /** Keeps all ui controls. */
    var uiControls: SwingTextField[];

    /** Returns input form ready for usage. */
    override protected function create () : Node {
       var labels:SwingLabel[];
       var x = 20;
       var y = 20;
       var dy = 18;     //shift between label and ui control
       var dyRow = 50;  //shift between rows

       for (formRow in formRows){
            insert createLabel(formRow.title, formRow.uiControl, x, y) into labels;

            formRow.uiControl.layoutX = x;
            formRow.uiControl.layoutY = y+=dy;
            insert formRow.uiControl into uiControls;
            y+=dyRow;
       }

       return Group{
        content: [labels,uiControls, createSubmitButton(x, y, "Добавить в справочник.")]
       }
    }

    /** Creates SwingLabel. */
    public function createLabel(title:String, labelFor : SwingComponent, x: Integer, y : Integer) : SwingLabel {
        return SwingLabel{
                    layoutX: x;
                    layoutY: y;
                    text: title,
                    labelFor: labelFor,
                    verticalAlignment: SwingVerticalAlignment.TOP
               }
    }

    public function createSubmitButton (x: Integer, y: Integer, title: String) : SwingButton{
        return SwingButton{
            layoutX: x
            layoutY: y
            text: title
            onMouseClicked: function(evt: MouseEvent): Void{
                                println("Save");
                                for(uiControl in uiControls){
                                    EntityReflector.setValue(model, uiControl.getName(), uiControl.getJTextField().getText());
                                }
                                println("save result:{model}");
                                //DataStoreManager.getDataStore(model).add(model);
                                var map : HashMap = new HashMap();
                                map.put(model.getId(), model);

                            }

        };;;;

    }



}
